'use strict';

const Controller = require('egg').Controller;
// //jsonwebtoken
// const jwt = require("jsonwebtoken");
//数据
const MockDate = require('../../mock')
//egg-mysql
const mysql = require('mysql');

class HomeController extends Controller {
  // //登录
  // async login() {
  //   const { ctx, app } = this;
  //   console.log(ctx.request.body);
  //   const { username, password } = ctx.request.body;
  //   const data = await app.mysql.get("login", {
  //     username,
  //     password,
  //   });
  //   console.log(data);
  //   if (data) {
  //     ctx.body = {
  //       code: "001",
  //       msg: "登录成功",
  //       token: jwt.sign(data.username, "1901B"),
  //     }

  //   } else {
  //     ctx.body = {
  //       code: "002",
  //       msg: "登录失败",
  //     }
  //   }
  // }
  // //注册
  // async setlogin() {
  //   const { ctx, app } = this;
  //   console.log(ctx.request.body);
  //   const { username, password } = ctx.request.body;
  //   const data = await app.mysql.get("list", {
  //     username,
  //     password,
  //   });
  //   if (data) {
  //     ctx.body = {
  //       code: "001",
  //       msg: "该账号已注册",
  //     };
  //   } else {
  //     try {
  //       await app.mysql.insert("list", {
  //         username: username,
  //         password: password,
  //       });
  //       ctx.body = {
  //         code: "001",
  //         msg: "该账号注册成功",
  //       };
  //     } catch (error) {
  //       ctx.body = {
  //         code: "002",
  //         msg: "注册失败",
  //       };
  //     }
  //   }
  // }
  //把数据推送数据库
  async index() {
    const { ctx, app } = this;
    await MockDate.forEach(item => {
      app.mysql.insert('mocklist', { url: item.url, title: item.title, desc: item.desc })

      ctx.body = {
        code: 200,
        msg: '成功'
      }
    })
  }
  //渲染 从后台那数据
  async getList() {
    const { ctx, app } = this;
    const data = await app.mysql.select('mocklist')

    ctx.body = {
      code: 200,
      data
    }
  }

  //详情
  async detail() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('mocklist', { id })

    ctx.body = {
      code: 1,
      data
    }
  }

  //筛选户型
  async screening3() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("mocklist", { where: { type: value } })

    if (value === '户型') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  };
   //筛选
   async screening4() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("mocklist", { where: { group: value } })

    if (value === '筛选') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }
//筛选区域
async screening1() {
  const { app, ctx } = this
  const { value } = ctx.request.body
  const data = await app.mysql.select("mocklist", { where: { city: value } })

  if (value === '区域') {
    ctx.body = {
      code: 1,
      data
    }
  } else {
    ctx.body = {
      code: 500,
      data
    }
  }
}


}

module.exports = HomeController;
